package com.jck.data.util;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.sql.DataSource;

 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

 
/**
 * spring bean 处理和公共工具类
 * 
 * @ClassName: SpringBeanUtil
 * @Description:
 * @author 作者 jacking
 * @date 2016-7-7 下午5:20:11
 */
@SuppressWarnings("all")
@Component("springBeanUtil") // 使用注解
public class SpringBeanUtil implements ApplicationContextAware {
	private static Logger logger = LoggerFactory.getLogger(SpringBeanUtil.class);
	private static String[] springXmlFile = new String[] { "classpath:/*Context*.xml" };
	private static ApplicationContext ctx = null;
	private static Map<String, Properties> propMap = new HashMap<String, Properties>(0);
	private static Map<String, Object> dataMap = new HashMap<String, Object>(0);
//	private static Map<String, String> syncIdMap = new HashMap<String, String>();
	private static ExecutorService pool;
	public static ClassPathXmlApplicationContext ioc;
	 
	private static Properties property;
	private static SpringBeanUtil sb = null;

	private SpringBeanUtil() {
	}

	public synchronized static SpringBeanUtil getInstance() {
		if (sb == null) {
			ioc=new ClassPathXmlApplicationContext(springXmlFile);
			Properties prop = new Properties();
			Resource resource =ctx.getResource("/config.properties");
			try {
				prop.load(resource.getInputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}
			property = prop;
			sb = new SpringBeanUtil();
		}
		return sb;
	}

 
	
	public static Map<String, Properties> getPropMap() {
		return propMap;
	}

	public static ExecutorService getPool() {
		return pool;
	}

 

	public static Properties getProperty() {
		return property;
	}

	public static Properties getProperty(String filePath) {
		Properties prop = new Properties();
		Resource resource = ctx.getResource(filePath);
		try {
			prop.load(resource.getInputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}
		property = prop;
		return property;
	}

	@Override
	public void setApplicationContext(ApplicationContext ctx) throws BeansException {
		this.ctx = ctx;
	}

	public static Object getBean(String prop) {
		Object obj = ctx.getBean(prop);
		if (logger.isDebugEnabled()) {
			logger.debug("property=[" + prop + "],object=[" + obj + "]");
		}
		return obj;
	}

	public static Properties getProperties(String filepath) {
		if (propMap.containsKey(filepath))
			return propMap.get(filepath);
		Resource resource = ctx.getResource(filepath);
		Properties prop = new Properties();
		try {
			prop.load(resource.getInputStream());
			propMap.put(filepath, prop);
			return prop;
		} catch (IOException e) {
			logger.error("can not find the resource file:[" + filepath + "]", e);
			return null;
		}
	}

	public static DataSource getDataSource(String source) {
		return (DataSource) getBean(source);
	}

 

	public static void main(String[] args) {
		// System.out.println(SpringBeanUtil.getInstance().getWebService());

		Map map = SpringBeanUtil.getInstance().getDataMap();
		System.out.println(map);
		// System.out.println(propMap);

		try {
			// prop.load(new FileInputStream(new File(tpath)));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static ApplicationContext getCtx() {
		return ctx;
	}

	public static void setCtx(ApplicationContext ctx) {
		SpringBeanUtil.ctx = ctx;
	}

	 

	public static SpringBeanUtil getSb() {
		return sb;
	}

	public static void setSb(SpringBeanUtil sb) {
		SpringBeanUtil.sb = sb;
	}

	public static String[] getSpringXmlFile() {
		return springXmlFile;
	}

	public static void setSpringXmlFile(String[] springXmlFile) {
		SpringBeanUtil.springXmlFile = springXmlFile;
	}

	public static void setPropMap(Map<String, Properties> propMap) {
		SpringBeanUtil.propMap = propMap;
	}

	public static void setPool(ExecutorService pool) {
		SpringBeanUtil.pool = pool;
	}

 

	public static void setProperty(Properties property) {
		SpringBeanUtil.property = property;
	}

	public static Map<String, Object> getDataMap() {
		return dataMap;
	}

	public static void setDataMap(Map<String, Object> dataMap) {
		SpringBeanUtil.dataMap = dataMap;
	}

	 

	 


}
